package healththink
import grails.plugins.springsecurity.*

class FacilitySvcController {
    @Secured(['ROLE_OWNER', 'ROLE_MODERATOR', 'IS_AUTHENTICATED_FULLY'])
    def scaffold = true
    
    def mobileService
    
    def afterInterceptor = [action: this.&handleMobile]
    
    private handleMobile(model, modelAndView) {
       
        
            if (modelAndView && mobileService.detect(request)) {

                modelAndView.viewName = '/mobile/' + modelAndView.viewName          

            }
	 
    }

   @Secured(['IS_AUTHENTICATED_ANONYMOUSLY'])
    def index() { 
        redirect(action: "list")
    }
    
    @Secured(['ROLE_OWNER', 'ROLE_MODERATOR', 'IS_AUTHENTICATED_FULLY'])
    def addNew = {
        if(!params.add){
            def facility = Facility.findByName(params.facilityName)
            [facility: facility]
        }else{
            def facility = Facility.findByName(params.facility.name)
            def service = new FacilitySvc(params)
            service.facility = facility
            facility.addToFacilitySvcs(service)
            if(mobileService.detect(request)){
                forward (controller: "facility", action: "view", params: [name: facility.name])
            }else{
                redirect(controller: "facility", action: "view", params: [name: facility.name])
            }
        }
    }
    
    @Secured(['IS_AUTHENTICATED_ANONYMOUSLY'])
    def view = {
        def facility = Facility.findByName(params.facilityName)
        def service = FacilitySvc.findByName(params.serviceName)
        [facility: facility, service: service]
    }
}
